package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.domain.StockReq;
import com.fan.shop.domain.StockReqLine;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StockReqService;
import com.fan.shop.services.StockService;
import com.fan.shop.services.StoreService;

public class StockReqAddToStockProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(StockReqAddToStockProcess.class);

	private StockReq stockReq;

	private StockReqService stockReqService;
	private StockReqLineService stockReqLineService;
	private StockService stockService;
	private ItemService itemService;
	private StoreService storeService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqAddToStockProcess.logger = logger;
	}

	public StockReq getStockReq() {
		return stockReq;
	}

	public void setStockReq(StockReq stockReq) {
		this.stockReq = stockReq;
	}

	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	@Override
	public Integer processImpl() {

		List<StockReqLine> stockReqLines = stockReqService
				.getStockReqLines(stockReq.getRequisitionNo());

		for (StockReqLine line : stockReqLines) {
			double avgCost = stockReq.getPostage().doubleValue()
					/ stockReqService.getTotalQty(stockReq.getRequisitionNo());
			// create new stock item
			if (!stockService.hasStock(line.getId().getItemCode(), line.getStoreCode())) {

				StockCreateProcess stockCreateProcess = new StockCreateProcess();
				stockCreateProcess.setItemCode(line.getId().getItemCode());
				stockCreateProcess.setStoreCode(line.getStoreCode());
				stockCreateProcess.setCost(new BigDecimal(avgCost));
				stockCreateProcess.setNetPrice(line.getPrice());
				stockCreateProcess.setQty(line.getQty());

				stockCreateProcess.setItemService(itemService);
				stockCreateProcess.setStockService(stockService);
				stockCreateProcess.setStoreService(storeService);

				int result = stockCreateProcess.process();
				if (result == ShopGlobalVariables.FAILED) {
					setErrorMessage(stockCreateProcess.getErrorMessage());
					return result;
				}
			}
			// update existing stock item
			else {
				StockUpdateProcess stockUpdateProcess = new StockUpdateProcess();
				stockUpdateProcess.setItemCode(line.getId().getItemCode());
				stockUpdateProcess.setStoreCode(line.getStoreCode());
				stockUpdateProcess.setCost(new BigDecimal(avgCost));
				stockUpdateProcess.setNetPrice(line.getPrice());
				stockUpdateProcess.setQty(line.getQty());

				stockUpdateProcess.setItemService(itemService);
				stockUpdateProcess.setStockService(stockService);
				stockUpdateProcess.setStoreService(storeService);

				int result = stockUpdateProcess.process();
				if (result == ShopGlobalVariables.FAILED) {
					setErrorMessage(stockUpdateProcess.getErrorMessage());
					return result;
				}
			}
		}
		
		return ShopGlobalVariables.SUCCESS;
	}

}
